<?php
namespace Home\Controller;

use Home\Logic\ChatRoomLogic;
use Home\Logic\LiaotianLogic;
use Home\Logic\NewsListLogic;
use Home\Logic\RoomLogic;
use Home\Logic\UserLogic;
use Home\Logic\WebConfigLogic;
use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {
        $fid = I("get.fid", 0);
        $psw = I("get.pwd");
        if ($fid > 0) {
            session("tuijianmid", $fid);
        }
        //判断是否为用户登录
        $denglu = I("denglu", 0);
        if ($denglu == 1) {
            session("denglu", $denglu);
        }

        //判断是否登陆
        $userLogic = new UserLogic();
        $roomLogic = new RoomLogic();
        //获取房间信息
        $roomInfo = $roomLogic->getRoomInfo($fid);
        if (!$roomInfo) {
            //获取第一个房间信息
            $firstRoom = $roomLogic->get_first_room();
            redirect(U("Home/Index/index/fid/{$firstRoom["fid"]}"));
        }
        $mid = max(intval(session("mid")), 0);
        //获取用户信息
        $userinfo = $userLogic->getUserInfo($mid, $fid);
        if ($userinfo["type"] == 1) {//游客登陆
            session("mid", $userinfo["mid"]);
            session("username", $userinfo['username']);
            session("leixing", "游客");
            session("fid", $fid);
        } else {//注册用户登录
            session("mid", $userinfo["mid"]);
            session("username", $userinfo['username']);
            session("leixing", $userinfo['adminname']);
            session("fid", $fid);
            session("adminid", $userinfo['adminidd']);
        }
        $chat_logic = new ChatRoomLogic();
        //获取推广的人数
        $total_tuijian = $userLogic->getTotalTuijian($userinfo["mid"]);
        if ($psw == $roomInfo['password'] && $userinfo['leixing'] != "老师") {
            //进入登陆页面
        }

        //获取房间配置信息
        $this->getRoomConfig($fid);
        //加载新闻广告信息
        $this->loadNewsList($fid);

        $now = time();
        //假造普通消息
        $this->loadCommonHistoryMsg($fid,$now);
        //加载高级消息
        $this->loadAdvanceHistoryMsg($fid,$now);

        $this->display();
    }

    /**
     * 配置房间信息
     * @param $fid
     */
    private function getRoomConfig($fid)
    {
        $webconfigLogic = new WebConfigLogic();
        $config = $webconfigLogic->getWebConfig($fid);
        $this->assign("config", $config);
    }

    private function loadNewsList($fid){
        $newsLogic = new NewsListLogic();
        $newslist = $newsLogic->getList($fid);
        $this->assign("ad_position",$newslist);
    }

    private function loadCommonHistoryMsg($fid,$time){
        $liaotianLogic = new LiaotianLogic();
        $data = $liaotianLogic->getCommonMsg($time,$fid,1);
        $this->assign("common_msg",$data);
    }

    private function loadAdvanceHistoryMsg($fid,$time){
        $liaotianLogic = new LiaotianLogic();
        $data = $liaotianLogic->getCommonMsg($time,$fid,2);
        $this->assign("advance_msg",$data);
    }

}